﻿using System;
using Com.PhilChuang.Mobile.Apps.QuickText.Ui.Views.Impl;
using Com.PhilChuang.Pmvp.Ui.Views.Impl.Pages;
using Com.PhilChuang.Pmvp.Ui.Views.Impl.ViewModels;

namespace Com.PhilChuang.Pmvp.Ui.Views.Impl.ViewFacades
{
	public class SendFeedbackViewFacade : AbstractViewFacade<SendFeedbackPage, SendFeedbackPageViewModel>, ISendFeedbackView, IReportBugView
	{
		#region Constants
		private const String DO_SEND_MESSAGE_PRESSED = "SendFeedbackViewFacade.DO_SEND_MESSAGE_PRESSED";
		private const String IS_BUG_REPORT = "SendFeedbackViewFacade.IS_BUG_REPORT";
		#endregion

		#region Events
		public event Action DoSendEmail = delegate { };
		#endregion

		#region Instance Variables (EMPTY)
		#endregion

		#region Properties
		public bool IsBugReport { get; set; }

		public String Prompt
		{
			get { return myViewModel.Prompt; }
			set
			{
				if (DoNotAccessViewModel ()) return;
				InvokeSet (val => {
				           	if (DoNotAccessViewModel ()) return;
				           	myViewModel.HasBeenLoaded = true;
				           	myViewModel.Prompt = val;
				           },
				           value);
			}
		}

		public String UserText
		{
			get { return myViewModel.UserText; }
			set
			{
				if (DoNotAccessViewModel ()) return;
				InvokeSet (val => {
				           	if (DoNotAccessViewModel ()) return;
				           	myViewModel.HasBeenLoaded = true;
				           	myViewModel.UserText = val;
				           },
				           value);
			}
		}
		#endregion

		#region Constructors
		public SendFeedbackViewFacade ()
		{
			Init (false);
		}

		public SendFeedbackViewFacade (SendFeedbackPage page)
		{
			myPage = page;
			Init (true);

			if (myPage.State.ContainsKey (IS_BUG_REPORT))
			{
				IsBugReport = (bool) myPage.State[IS_BUG_REPORT];
				myPage.State.Remove (IS_BUG_REPORT);
			}
		}

		private void Init (bool wireUp)
		{
			IsBugReport = false;
			myViewModel = new SendFeedbackPageViewModel ();
			myViewModel.IsAssociatedWithViewFacadeAndPage = true;
			myViewModel.HasBeenLoaded = false;
			if (wireUp) WireUpPage ();
		}
		#endregion

		/// <summary>
		/// Unsubscribes view event handlers.
		/// Call base impl as last step.
		/// </summary>
		protected override void UnWirePage ()
		{
			if (myPage == null) return;

			// ISendFeedbackView
			myPage.DoSendEmail -= FireDoSendEmail;
			// PageBase
			myPage.Shown -= OnShown;

			base.UnWirePage ();
		}

		/// <summary>
		/// Subscribes view event handlers.
		/// Call base impl as first step.
		/// </summary>
		protected override void WireUpPage ()
		{
			if (myPage == null) return;

			base.WireUpPage ();

			// ISendFeedbackView
			myPage.DoSendEmail += FireDoSendEmail;
			// PageBase
			myPage.Shown += OnShown;
		}

		protected void FireDoSendEmail ()
		{
			myPage.State[DO_SEND_MESSAGE_PRESSED] = true;
			myPage.State[IS_BUG_REPORT] = IsBugReport;
			DoSendEmail ();
		}

		protected void OnShown ()
		{
			// get state about send message pressed);
			if (myPage.State.ContainsKey (DO_SEND_MESSAGE_PRESSED))
			{
				var sendMessagePressed = (bool) myPage.State[DO_SEND_MESSAGE_PRESSED];
				myPage.State[DO_SEND_MESSAGE_PRESSED] = false; // removing it doesn't seem to persist upon the next page load
				if (sendMessagePressed)
				{
					DisplayMessage ("Your feedback is appreciated!", "Thanks");
					Close ();
				}
			}
		}

		public void FocusOnUserTextControl ()
		{
			if (IsDisposedOrDisposing ()) return;

			WaitForShownThenBeginInvokeAsync (() => myPage.FocusOnUserTextControl (), ViewImplConstants.FOCUS_DELAY_MS);
		}
	}
}
